/**
 *
 */
package com.engine.Mangers;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.util.EngineUtil;
import com.wfe.db.SessionUtil;
import com.wfe.mapping.client.WfProjectTask;
import com.wfe.mapping.client.WfProjectTaskStatus;

/**
 * @author kareem_yehya
 * 
 */
public class RepairTaskStatus7 implements Runnable
{
	private static int fetchSize = 50;
	private static String fetchSizeParameter = "";

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run()
	{
		while(true)
		{
			Session session = null;
			try
			{
				session = SessionUtil.getSession();
				// ------------------------- assigned process tasks
				System.out.println("Resume Repair Problems main Thread");
				List<WfProjectTask> taskList = loadTasksNeedRepair(fetchSize, session);

				if(taskList.size() > 0)
				{
					System.out.println("Inside IF");
					for(WfProjectTask wfProjectTask : taskList)
					{
						updateTask(wfProjectTask, session);

					}
				}
				Thread.sleep(100);
			}catch(Exception e)
			{
				e.printStackTrace();
			}finally
			{
				EngineUtil.closeSession(session);
			}
		}

	}

	private void updateTask(WfProjectTask wfProjectTask, Session session)
	{

		try
		{
			WfProjectTaskStatus status = getWfProjectTaskStatusByID(4l, session);// assigned
			wfProjectTask.setIsRouted(true);
			wfProjectTask.setRouteTime(null);
			wfProjectTask.setIsProcessed(false);
			wfProjectTask.setWfProjectTaskStatus(status);
			updateWfProjectTask(wfProjectTask, session);
		}catch(Exception e)
		{
			e.printStackTrace();
		}

	}

	public static void updateWfProjectTask(WfProjectTask task, Session mySession)
	{

		System.out.println("Task Id = " + task.getRecid());
		mySession.update(task);
		mySession.beginTransaction().commit();

	}

	public static WfProjectTaskStatus getWfProjectTaskStatusByID(long i, Session session)
	{

		WfProjectTaskStatus wfProjectTaskStatus = null;
		try
		{

			Criteria criteria = session.createCriteria(WfProjectTaskStatus.class);
			criteria.add(Restrictions.eq("recid", new Long(i)));
			wfProjectTaskStatus = (WfProjectTaskStatus) criteria.list().get(0);
			return wfProjectTaskStatus;

		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}

	}

	@SuppressWarnings("unchecked")
	private List<WfProjectTask> loadTasksNeedRepair(int maxresults, Session session)
	{
		List<WfProjectTask> tasklist = null;

		// --------------------------------------------
		System.out.println("Inside");
		// -------------------------------------------------------------

		try
		{

			WfProjectTaskStatus wfProjectTaskStatus = getWfProjectTaskStatusByID(7l, session); // Problem

			Criteria criteria = session.createCriteria(WfProjectTask.class);

			if(maxresults > 0)
			{
				criteria.setMaxResults(maxresults);
			}
			criteria.add(Restrictions.eq("wfProjectTaskStatus", wfProjectTaskStatus));// Problem
			tasklist = criteria.list();

			return tasklist;
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		if(args.length > 0)
		{
			fetchSizeParameter = args[0];
			if(fetchSizeParameter != null && !fetchSizeParameter.equals(""))
			{
				fetchSize = Integer.parseInt(fetchSizeParameter);
				System.out.println("fetchSize = " + fetchSize);
			}
		}

		(new Thread(new RepairTaskStatus7())).start();

	}

}
